
#include "key_mouse.h"


static  int key_mouse_fd = 0;

static pthread_t   key_mouse_thread;



int key_mouse_read_event(void)
{
	int ret = -1;
	struct input_event event;

	if(key_mouse_fd <= 0)
		return -1;
	
	ret = read(key_mouse_fd, &event, sizeof(event));
	if(ret <= 0){
		return -1;
	}

	
	//log_i("type=%d\n",event.type);
	//log_i("code=%d\n",event.code);
	//log_i("value=%d\n",event.value);

	if((event.type == EV_KEY)&&(event.code == BTN_LEFT))
	{
		//log_i("value=%d\n",event.value);
		//log_i("value=%d\n",event.value);

		if(event.value) 
		{ 
			log_i("left_down\n");

			ffmpeg_set_msg(1);
		}
		else
		{
			log_i("left_up\n");
		}
	}
	if((event.type == EV_KEY)&&(event.code == BTN_RIGHT))
	{
		//log_i("value=%d\n",event.value);
		//log_i("value=%d\n",event.value);

		if(event.value) 
		{ 
			log_i("right_down\n");
			ffmpeg_set_msg(1);
		}		
		else
		{
			log_i("right_up\n");
		}
	}	


	return 0;
}



void* key_cat_thread(void *arg)
{
	int       packet_size = 0;
	uint8_t * packet_data = NULL;
	

	while(1)
	{
		usleep(1000);

		key_mouse_read_event();

	}

}


int key_mouse_init(void)
{	
	int ret = -1;
	
	key_mouse_fd     = open(DEV_KEY_MOUSE_NAME, O_RDONLY|O_NONBLOCK);
	if(key_mouse_fd <= 0){
		log_e("open mouse error\n");
		return -1;
	}

	ret=pthread_create(&key_mouse_thread,NULL,key_cat_thread,NULL);
	if(ret != 0){
		log_e("thread error=%s\n",strerror(ret));
	}	
	
	
	return 0;
}



int key_mouse_deinit(void)
{
	if(key_mouse_fd){
		close(key_mouse_fd);
		key_mouse_fd = 0;
	}
	
	
	
	return 0;
}
